package com.skynet.supervision.utils;

import lombok.Data;
import lombok.SneakyThrows;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

@SuppressWarnings("unused")
public abstract class TimeUtils {
    public static final String DefaultTimeShortFormat = "yyyy-MM-dd";
    public static final String DefaultTimeFormat = "yyyy-MM-dd HH:mm:ss";

    @SneakyThrows
    public static long nowUnix() {
        return Instant.now().getEpochSecond();
    }

    /**
     * 日期字符串转换为时间戳
     *
     * @param fmt     格式
     * @param dateStr 日期字符
     * @return 时间戳
     */
    @SneakyThrows
    public static long toTimeStamp(String fmt, String dateStr) {
        return new SimpleDateFormat(fmt)
                .parse(dateStr).getTime();
    }

    public static Long toTimeStamp(LocalDateTime ldt) {
        return ldt.toInstant(ZoneOffset.of("+8")).getEpochSecond();
    }

    /**
     * 系统短时间格式
     *
     * @see #toTimeStamp(String, String)
     */
    public static long toTimeStampWithShortFormat(String dateStr) {
        return toTimeStamp(DefaultTimeShortFormat, dateStr);
    }

    /**
     * 系统默认时间格式
     *
     * @see #toTimeStamp(String, String)
     */
    public static long toTimeStampWithDefaultFormat(String dateStr) {
        return toTimeStamp(DefaultTimeFormat, dateStr);
    }

    /**
     * @see #format(Date, String)
     */
    public static @NotNull String format(long timeStamp, String fmt) {
        return format(new Date(timeStamp), fmt);
    }

    /**
     * 通用格式化时间
     *
     * @param date 时间
     * @param fmt  时间格式
     */
    public static @NotNull String format(Date date, String fmt) {
        return new SimpleDateFormat(fmt)
                .format(date);
    }


    /**
     * @see #fmtSystemDefaultFormat(Date)
     */
    public static @NotNull String fmtSystemDefaultFormat(long timeStamp) {
        return fmtSystemDefaultFormat(new Date(timeStamp));
    }

    /**
     * 秒级时间戳转date
     * @param timeStamp
     * @return
     */
    public static @NotNull String fmtSystemDefaultFormatMtoDate(long timeStamp) {
        Long time = new Long(new Long(timeStamp)*1000);
        return fmtSystemDefaultFormat(new Date(time));
    }


    /**
     * 使用系统默认时间格式进行格式化
     *
     * @see #format(Date, String)
     */
    public static @NotNull String fmtSystemDefaultFormat(Date date) {
        return format(date, DefaultTimeFormat);
    }


    /**
     * @see #fmtSystemShortFormat(Date)
     */
    public static @NotNull String fmtSystemShortFormat(long timeStamp) {
        return fmtSystemShortFormat(new Date(timeStamp));
    }

    /**
     * 使用系统短时间格式进行格式化
     *
     * @see #format(Date, String)
     */
    public static @NotNull String fmtSystemShortFormat(Date date) {
        return format(date, DefaultTimeShortFormat);
    }

    /**
     * date转loacldatetime
     */
    public static LocalDateTime dateToLcalDate(Date date){
        LocalDate now = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDateTime nowTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return nowTime;
    }

    /**
     *localdatetime转date
     */
    public static Date localDateToDate(LocalDateTime localDateTime){
        Date date;
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * long转date
     */
    @SneakyThrows
    public static Date longToDate(long lo)  {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //long转Date
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sd.format(new Date(lo)));
        return date;
    }

    /**
     * sting转date
     */
    @SneakyThrows
    public static Date stringToDate(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(str);
        return date;
    }


    /**
     * 获取当前的系统时间，以秒为单位, java.util.Date.Date()
     */
    @SneakyThrows
    public static long currentTime()
    {
        return new Date().getTime()/1000;	//获取当前时间的秒数值
    }


    /**
     * 获得某天最大时间 到秒 Long
     */
    public static Long getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()).getTime()/1000;
    }

    /**
     * 获得某天最小时间 到秒 Long
     */
    public static Long getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()).getTime()/1000;
    }

}
